package org.dlsu.mt.parser.generation;
import java.util.HashSet;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import org.dlsu.mt.parser.FStructure;
import org.dlsu.mt.parser.FStructureElement;
import org.dlsu.mt.parser.FStructureElementValue;
import org.dlsu.mt.parser.FStructureInterface;
import org.dlsu.mt.parser.dictionary.Global;

public class HTPLConverter
{
	private FStructure f;
	private GrammarList list;
	
	
	public HTPLConverter()
	{
		f = null;
		list = null;
	}
	
	public HTPLConverter(FStructure f)
	{
		this.f = f;
		list = null;
	}
	
	public HTPLConverter(GrammarList list)
	{
		f = null;
		this.list = list;
	}
	
	public HTPLConverter(FStructure f, GrammarList list)
	{
		this.f = f;
		this.list = list;
	}
	
	public void loadGrammarList(GrammarList list)
	{
		this.list = list;
	}
	
	public Phrase convert()
	{
		if(f == null)
			return null;
		else
		{
			//convert
			Global.logWriter.println("HTPL Converter Module now starting!!");
			Global.logWriter.println("Converting the root FStructure");
			Phrase p = new Phrase();
			Vector<String> keys = getSortedKeys(f);
			for(int i=0;i<keys.size();i++)
			{
				String key = keys.get(i);
				if(f.getEntry(key) instanceof FStructure)
				{
					FStructure elem = (FStructure)f.getEntry(key);
					p.addElement(convertPhraseContent(elem));
				}
				else if(f.getEntry(key) instanceof FStructureElement)
				{
					FStructureElement elem = (FStructureElement)f.getEntry(key);
					p.addElement(convertPotentialWord(elem));
				}
			}
			Global.logWriter.println("finished converting root FStructure");
			return p;
		}
	}
	
	public Phrase convert(FStructure f)
	{
		this.f = f;
		return convert();
	}
	
	public PhraseContent convertPhraseContent(FStructure f)
	{
		PhraseList list = new PhraseList();
		
		Global.logWriter.println("converting " + f.getName() + " FStructure");
		
		Vector<String> keys = getSortedKeys(f);
		if(keys!=null)
			for(int i = 0; i<keys.size();i++)
			{
				String key = keys.get(i);
				FStructureInterface elem = f.getEntry(key);
				if(elem instanceof FStructure)
				{
					PhraseContent temp = convertPhraseContent((FStructure)elem);
					if(temp != null)
						list.addElement(temp);
				}
				else if(elem instanceof FStructureElement)
				{
					list.addElement(convertPotentialWord((FStructureElement)elem));
				}
			}
		Global.logWriter.println("finished converting " + f.getName() + " FStructure");
		if(keys != null)
			return new PhraseContent(f.getName(), list);
		else
			return null;
	}

	public PotentialWord convertPotentialWord(FStructureElement e)
	{	
		HashSet values = e.getValues();
		Iterator i = values.iterator();
		while(i.hasNext())
		{
			String temp = ((FStructureElementValue) i.next()).getValue().toString();
			if(temp.length()>5&&temp.substring(0,4).toLowerCase().equals("pred"))
			{
				StringTokenizer x= new StringTokenizer(temp,"\t");
				while(x.hasMoreTokens())
				{
					temp = x.nextToken();
					Global.logWriter.println("finished converting " + e.getName() + " FStructureElement");
					if(!temp.toLowerCase().equals("pred"))
						return new PotentialWord(e.getName(),temp);
				}
			}
		}
		Global.logWriter.println("finished converting " + e.getName() + " FStructureElement");
		return null;
	}
	
	public Vector<String> getSortedKeys(FStructure f)
	{
		Vector<String> keys = f.getKeys();
		
		Global.logWriter.println("items to be converted are:");
		if (keys == null)
			System.out.println("GIAN keys are null");			
		for(int i=0;i<keys.size();i++)
			Global.logWriter.println(keys.get(i));
		Global.logWriter.println();
		
		if(keys != null)
			keys= list.order(keys,f.getName());
		
		Global.logWriter.println("proper order as to how they should appear is:");
		for(int i=0;i<keys.size();i++)
			Global.logWriter.println(keys.get(i));
		Global.logWriter.println();
		
		return keys;
	}
	

}